<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Api\Repositories\WxCircleRepositores;
use App\Lib\JwtAuth;
use Illuminate\Http\Request;
use App\Models\Posts\WxUserCircle;
use App\Models\WxUser;


class CircleController extends BaseController
{
    /**
     * 推荐
     * @return \Illuminate\Http\JsonResponse
     */
    public function recommendCircle()
    {
        return $this->success(WxCircleRepositores::recommendCircle());
    }

    /**
     * 热门
     * @return \Illuminate\Http\JsonResponse
     */
    public function hotCircle()
    {
        return $this->success(WxCircleRepositores::hotCircle());
    }

    /**
     * 最新
     * @return \Illuminate\Http\JsonResponse
     */
    public function newCircle()
    {
        return $this->success(WxCircleRepositores::newCircle());
    }

    /**
     * 获取全部
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        $page = $request->page;
        return $this->success(WxCircleRepositores::list($page));
    }

    /**
     * 获取圈子组合列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function circleAndPosts(Request $request)
    {
        $limit = $request->input('limit', 10);
        return $this->success(WxCircleRepositores::circleAndPost($limit));
    }

    /**
     * 用户关注圈子
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function followCircle(Request $request)
    {
        $uid = $request->uid;
        $circle_id = $request->circle_id;
        return $this->success(WxCircleRepositores::userFollowCircle($uid, $circle_id));

    }

    /**
     * 用户关注圈子文章组合列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function userFollowCircleList(Request $request)
    {
        $uid = $request->uid;
        $limit = $request->input('limit', 10);
        return $this->success(WxCircleRepositores::userFollowCircleList($limit, $uid));
    }

    /**
     * 获取圈子详细
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCircleInfoById(Request $request)
    {
        $circle_id = $request->circle_id;
        $token = $request->header('token');
        $uid = '';
        if ($token) {
            $uid = JwtAuth::getUserId($token, 'uid');
        }
        $data = WxCircleRepositores::CircleInfo($circle_id);
        $data->is_follow_circle = WxCircleRepositores::isFollowCircle($uid, $circle_id);
        return $this->success($data);
    }

    public function getCircleUserList(Request $request)
    {
        $circle_id = $request->circle_id;

        $circleUserList = WxUserCircle::where('circle_id', $circle_id)->get();
        $user_id_arr = [];
        foreach ($circleUserList as $k => $v) {
            $user_id_arr[] = $v['user_id'];
        }

        $data = WxUser::whereIn('id', $user_id_arr)
            ->get(['user_avatar', 'user_name', 'id']);

        return $this->success($data);
    }

}
